//
//  BHAccountTool.m
//  xinlangweibo0621
//
//  Created by boldseas on 2017/6/25.
//  Copyright © 2017年 renbohan. All rights reserved.
//

#import "BHAccountTool.h"
#import "BHAccount.h"

#define BHAccountPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.date"]

@implementation BHAccountTool

+ (void)saveAccount:(BHAccount *)account{
    account.created_time = [NSDate date];
    
    [NSKeyedArchiver archiveRootObject:account toFile:BHAccountPath];
}

+ (BHAccount *)account{
    BHAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:BHAccountPath];
    
    long long expires_in = [account.expires_in longLongValue];
    NSDate *expires_time = [account.created_time dateByAddingTimeInterval:expires_in];
    NSDate *now = [NSDate date];
    
    NSComparisonResult result = [expires_time compare:now];
    if (result != NSOrderedDescending) {
        return nil;
    }
    
    return account;
}




@end
